package com.itheima.imclient100.presenter.impl;

import com.hyphenate.chat.EMClient;
import com.hyphenate.exceptions.HyphenateException;
import com.itheima.imclient100.db.DBUtils;
import com.itheima.imclient100.presenter.ContactPresenter;
import com.itheima.imclient100.utils.ThreadUtils;
import com.itheima.imclient100.view.ContactView;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Created by fullcircle on 2017/7/3.
 *
 *  联系人的impl只是简单的添加联系人
 */

public class ContactPresenterImpl implements ContactPresenter {
    private ContactView contactView;

    public ContactPresenterImpl(ContactView contactView) {
        this.contactView = contactView;
    }

    @Override
    public void initContacts(String username) {
        //从数据库获取数据
        List<String> contacts = DBUtils.initContacts(username);
        //利用数据库获取的数据更新界面
        contactView.onInitContacts(contacts);
        //联网获取最新的联系人数据
        updateContacts(username);
    }

    @Override
    public void updateContacts(final String username) {
        ThreadUtils.runOnSubThread(new Runnable() {
            @Override
            public void run() {
                try {
                    //getAllContactsFromServer 从环信服务器获取当前联系人的所有好友
                    final List<String> contacts = EMClient.getInstance().contactManager().getAllContactsFromServer();
                    //对集合按字母顺序进行排序
                    //不止是treemap能进行排序，普通的list集合也是能够进行排序的
                    Collections.sort(contacts, new Comparator<String>() {
                        @Override
                        public int compare(String o1, String o2) {
                            return o1.compareTo(o2);
                        }
                    });
                    //更新数据库
                    DBUtils.updateContacts(username,contacts);
                    ThreadUtils.runOnMainThread(new Runnable() {
                        @Override
                        public void run() {
                            //通知界面更新数据
                            contactView.onUpdateContacts(true,contacts,null);
                        }
                    });

                } catch (final HyphenateException e) {
                    e.printStackTrace();
                    ThreadUtils.runOnMainThread(new Runnable() {
                        @Override
                        public void run() {
                            //通知界面更新数据
                            contactView.onUpdateContacts(false,null,e.getMessage());
                        }
                    });
                }
            }
        });

    }

    @Override
    public void deleteContact(final String username) {
        ThreadUtils.runOnSubThread(new Runnable() {
            @Override
            public void run() {
                try {
                    //deleteContact删除好友
                    EMClient.getInstance().contactManager().deleteContact(username);
                    ThreadUtils.runOnMainThread(new Runnable() {
                        @Override
                        public void run() {
                            contactView.onDeleteContact(true,null);
                        }
                    });
                } catch (final HyphenateException e) {
                    e.printStackTrace();
                    ThreadUtils.runOnMainThread(new Runnable() {
                        @Override
                        public void run() {
                            contactView.onDeleteContact(false,e.getMessage());
                        }
                    });
                }
            }
        });

    }
}
